<?php

namespace App\Http\Controllers;

use App\Models\Video;
use App\Models\Category;
use App\Models\Banner;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class VideoController extends Controller
{
    /**
     * 获取视频列表
     */
    public function index(Request $request)
    {
        $query = Video::with(['category', 'user'])->where('is_active', true);
        
        // 分类筛选
        if ($request->has('category_id')) {
            $query->where('category_id', $request->category_id);
        }
        
        // 搜索
        if ($request->has('search')) {
            $query->where(function($q) use ($request) {
                $q->where('title', 'like', '%' . $request->search . '%')
                  ->orWhere('description', 'like', '%' . $request->search . '%');
            });
        }
        
        // 排序
        $sortBy = $request->get('sort_by', 'created_at');
        $order = $request->get('order', 'desc');
        $query->orderBy($sortBy, $order);
        
        // 分页
        $perPage = $request->get('per_page', 12);
        $videos = $query->paginate($perPage);
        
        return response()->json([
            'success' => true,
            'data' => $videos
        ]);
    }
    
    /**
     * 获取视频详情
     */
    public function show($id)
    {
        $video = Video::with(['category', 'user', 'comments.user'])
            ->where('is_active', true)
            ->findOrFail($id);
        
        return response()->json([
            'success' => true,
            'data' => $video
        ]);
    }
    
    /**
     * 获取首页推荐视频
     */
    public function recommended()
    {
        // 使用缓存，缓存5分钟
        $videos = Cache::remember('videos_recommended', 300, function () {
            return Video::with(['category'])
                ->where('is_active', true)
                ->where('is_featured', true)
                ->orderBy('views', 'desc')
                ->take(8)
                ->get();
        });
        
        return response()->json([
            'success' => true,
            'data' => $videos
        ]);
    }
    
    /**
     * 获取轮播图
     */
    public function banners()
    {
        // 使用缓存，缓存5分钟
        $banners = Cache::remember('videos_banners', 300, function () {
            return Banner::active()->ordered()->take(3)->get()->map(function ($banner) {
                return [
                    'id' => $banner->id,
                    'title' => $banner->title,
                    'description' => $banner->description,
                    'image' => $banner->image_url,
                    'link' => $banner->link || '/detail/' . $banner->id
                ];
            });
        });
        
        return response()->json([
            'success' => true,
            'data' => $banners
        ]);
    }

    /**
     * 增加播放次数
     */
    public function incrementViews($id)
    {
        $video = Video::findOrFail($id);
        $video->increment('views');
        
        return response()->json([
            'success' => true,
            'views' => $video->views
        ]);
    }

    /**
     * 点赞视频
     */
    public function toggleLike($id)
    {
        $video = Video::findOrFail($id);
        $video->increment('likes');
        
        return response()->json([
            'success' => true,
            'likes' => $video->likes
        ]);
    }

    /**
     * 获取视频评论
     */
    public function getComments($id)
    {
        $comments = Comment::with('user')
            ->where('video_id', $id)
            ->where('is_approved', true)
            ->orderBy('created_at', 'desc')
            ->get();
        
        return response()->json([
            'success' => true,
            'data' => $comments
        ]);
    }

    /**
     * 添加评论
     */
    public function addComment(Request $request, $id)
    {
        $request->validate([
            'content' => 'required|string|max:1000'
        ]);

        $comment = Comment::create([
            'content' => $request->content,
            'video_id' => $id,
            'user_id' => 1, // 临时使用用户ID 1，实际应该从认证中获取
            'is_approved' => true
        ]);

        $comment->load('user');

        return response()->json([
            'success' => true,
            'message' => '评论发表成功',
            'data' => $comment
        ]);
    }

    /**
     * 点赞评论
     */
    public function likeComment($id)
    {
        $comment = Comment::findOrFail($id);
        $comment->increment('likes');
        
        return response()->json([
            'success' => true,
            'likes' => $comment->likes
        ]);
    }
}

